import { GCellRenderer } from "../../../../../com/mui/controls/GCellRenderer";
import { NetDispatcher } from "../../../../mvc/core/NetDispatcher";
import { ServerCommand } from "../../../mvc/ServerCommand";
import { SkillInfo } from "../../../model/SkillInfo";
import { Cache } from "../../../cache/Cache";
import { EGuildStructureType } from "../../../../../Message/Public/EGuildStructureType";
import { Language } from "../../../../../extend/language/Language";
import { ObjCreate } from "../../common/ObjCreate";
import { ImagesConst } from "../../../resource/ImagesConst";
import { GuildSkillItem } from "./GuildSkillItem";
import { GlobalStyle } from "../../../../common/global/GlobalStyle";
import { GameConst } from "../../../../component/gconst/GameConst";
import { MsgManager } from "../../../manager/MsgManager";
import { ECategory } from "../../../../../Message/Public/ECategory";
import { EProp } from "../../../../../Message/Public/EProp";
import { PulseSharedObject } from "../../player/PulseSharedObject";
import { Alert } from "../../../../../com/mui/controls/Alert";
import { CheckBoxWin } from "../../common/alertwins/CheckBoxWin";
import { Dispatcher } from "../../../../mvc/core/Dispatcher";
import { DataEvent } from "../../../events/DataEvent";
import { EventName } from "../../../mvc/EventName";
import { SPlayerGuildInfo } from "../../../../../Message/Game/SPlayerGuildInfo";
import { GButton } from "../../../../../com/mui/controls/GButton";
import { TSkill } from "../../../../../Message/Db/Tables/TSkill";
type int = number;
//class GuildSkillPrivateCellRenderer
    
    export  class GuildSkillPrivateCellRenderer extends GCellRenderer
    {
        constructor()
        {
            super();
            this.init();
            NetDispatcher.addCmdListener(ServerCommand.SkillGuildLearned, this.onSkillGuildLearnedHandler);
            return;
        }

        public /* override */ set data(arg1: Object)
        {
            super.data = arg1;
            this._skillInfo = arg1 as SkillInfo;
            this._skillItem.skillInfo = this._skillInfo;
            this._skill = this._skillInfo.tSkill;
            if (this._skill) 
            {
                if (this._skillInfo.nextSkill) 
                {
                    this._studyCostContri = this._skillInfo.nextSkill.needGuildContribution;
                    this._needSchoolLevel = this._skillInfo.nextSkill.guildSchoolLevel;
                    this.showTip(false);
                }
                else 
                {
                    this.showTip(true);
                }
                this._contribution = Cache.instance.guild.myGuildInfo.contribution;
                this._schoolLevel = Cache.instance.guild.getStructureLevel(EGuildStructureType.convert(EGuildStructureType._EGuildSchool));
                this._maxStudyNum = Cache.instance.guild.getMaxSkillStudyTime();
                this._nameTxt.htmlText = this._skill.skillName;
                if (this._contribution >= this._studyCostContri) 
                {
                    this._costTxt.textColor = 15859633;
                }
                else 
                {
                    this._costTxt.textColor = 16711680;
                }
                this._costTxt.htmlText = this._studyCostContri.toString();
            }
            if (this._skillInfo.learned) 
            {
                this._mask.visible = false;
                this._levelTxt.htmlText = Language.getStringByParam(57579, this._skill.skillLevel);
            }
            else 
            {
                this._mask.visible = true;
                this._levelTxt.htmlText = Language.getStringByParam(57579, 0);
            }
            return;
        }

        /* internal  */showTip(arg1: Boolean): void
        {
            this._costLabelTxt.visible = !arg1;
            this._costTxt.visible = !arg1;
            this._studyBtn.visible = !arg1;
            this._tipTxt.visible = arg1;
            return;
        }

        /* internal  */init(): void
        {
            var loc1=85;
            var loc2=150;
            ObjCreate.createBg(0, 0, 190, 80, this, ImagesConst.DisabledBg);
            ObjCreate.createBg(10, 8, 66, 66, this, ImagesConst.PackItemBg);
            this._skillItem = new GuildSkillItem();
            this._skillItem.setSize(60, 60);
            this._skillItem.move(12, 10);
            this.addChild(this._skillItem);
            this._mask = new Object /* flash.display.Sprite */();
            this._mask.mouseEnabled = false;
            this._mask.graphics.beginFill(0, 0.6);
            this._mask.graphics.drawRect(12, 10, 60, 60);
            this._mask.graphics.endFill();
            this._mask.cacheAsBitmap = true;
            this.addChild(this._mask);
            this._nameTxt = ObjCreate.createTextField("", loc1, 12, 200, 20, this, GlobalStyle.textFormat6);
            this._nameTxt.autoSize = flash.text.TextFieldAutoSize.LEFT;
            this._levelTxt = ObjCreate.createTextField("0", loc2, 12, 200, 20, this, GlobalStyle.textFormatGreen);
            this._levelTxt.autoSize = flash.text.TextFieldAutoSize.LEFT;
            this._costLabelTxt = ObjCreate.createTextField(Language.getString(57562), loc1, 30, 200, 20, this);
            this._costTxt = ObjCreate.createTextField("", loc2, 30, 200, 20, this, GlobalStyle.textFormatOrange);
            this._costTxt.autoSize = flash.text.TextFieldAutoSize.LEFT;
            this._studyBtn = ObjCreate.createGButton(Language.getString(57563), 105, 50, 50, 20, this);
            this._studyBtn.addEventListener(flash.events.MouseEvent.CLICK, this.onStudyBtnClickHandler);
            this._studyBtn.drawNow();
            this._tipTxt = ObjCreate.createTextField(Language.getString(57580), loc1, 40, 200, 20, this, GlobalStyle.textFormatGreen);
            this._tipTxt.autoSize = flash.text.TextFieldAutoSize.LEFT;
            return;
        }

        /* internal  */onStudyBtnClickHandler(arg1: Object /* flash.events.Event */): void
        {
            var e: Object /* flash.events.Event */;
            var isNeedAlert: Boolean;
            var onBtnClickHandler: Function;

            var loc1;
            isNeedAlert = false;
            e = arg1;
            if (this._skill && this._skill.skillLevel < GameConst.MaxGuildSkillLevel) 
            {
                if (this._schoolLevel < this._needSchoolLevel) 
                {
                    MsgManager.showRollTipsMsg(Language.getString(57564));
                    return;
                }
                if (this._contribution < this._studyCostContri) 
                {
                    MsgManager.showRollTipsMsg(Language.getString(57565));
                    return;
                }
                this._studyNum = Cache.instance.guild.skillStudyTime;
                if (this._studyNum < this._maxStudyNum) 
                {
                    this.study();
                }
                else 
                {
                    this._propNum = Cache.instance.pack.backPackCache.getItemCountByCategoryAndType(ECategory._ECategoryProp, EProp._EPropGuildSkillLearn);
                    isNeedAlert = !PulseSharedObject.isTodayNotTips("GuildSkillStudy");
                    if (isNeedAlert) 
                    {
                        onBtnClickHandler = (arg1: int, arg2: Boolean): void
                        {
                            if (arg2) 
                            {
                                PulseSharedObject.save("GuildSkillStudy", true);
                            }
                            if (arg1 == Alert.OK) 
                            {
                                if (_propNum == 0) 
                                {
                                    MsgManager.showRollTipsMsg(Language.getString(57567));
                                    return;
                                }
                                study();
                                Alert.resetToDefault();
                            }
                            return;
                        }
                        Alert.alertWinRenderer = CheckBoxWin;
                        Alert.show(Language.getString(57566), null, Alert.OK | Alert.CANCEL, null, onBtnClickHandler);
                    }
                    else 
                    {
                        if (this._propNum == 0) 
                        {
                            MsgManager.showRollTipsMsg(Language.getString(57567));
                            return;
                        }
                        this.study();
                    }
                }
            }
            return;
        }

        /* internal  */study(): void
        {
            if (this._skillInfo.learned) 
            {
                Dispatcher.dispatchEvent(new DataEvent(EventName.skillUpgradeReq, this._skill.skillId));
            }
            else 
            {
                Dispatcher.dispatchEvent(new DataEvent(EventName.skillLearnReq, this._skill.skillId));
            }
            this._studyBtn.enabled = false;
            flash.utils.setTimeout(this.enableButton, 1000);
            return;
        }

        /* internal  */enableButton(): void
        {
            this._studyBtn.enabled = true;
            return;
        }

        /* internal  */onGuildUpdateInfoHandler(arg1: SPlayerGuildInfo): void
        {
            if (arg1) 
            {
                this._contribution = arg1.contribution;
                this._schoolLevel = Cache.instance.guild.getStructureLevel(EGuildStructureType.convert(EGuildStructureType._EGuildSchool));
                this._maxStudyNum = Cache.instance.guild.getMaxSkillStudyTime();
            }
            return;
        }

        /* internal  */onSkillGuildLearnedHandler(arg1: SkillInfo): void
        {
            if (this._skillInfo && arg1 && this._skillInfo.skillId == arg1.skillId) 
            {
                this.data = arg1;
                this._studyBtn.enabled = true;
            }
            return;
        }

        /* internal */ /* var */_mask: Object /* flash.display.Sprite */;

        /* internal */ /* var */_skillItem: GuildSkillItem;

        /* internal */ /* var */_nameTxt: Object /* flash.text.TextField */;

        /* internal */ /* var */_levelTxt: Object /* flash.text.TextField */;

        /* internal */ /* var */_costLabelTxt: Object /* flash.text.TextField */;

        /* internal */ /* var */_costTxt: Object /* flash.text.TextField */;

        /* internal */ /* var */_studyBtn: GButton;

        /* internal */ /* var */_tipTxt: Object /* flash.text.TextField */;

        /* internal */ /* var */_skillInfo: SkillInfo;

        /* internal */ /* var */_skill: TSkill;

        /* internal */ /* var */_studyCostContri: int;

        /* internal */ /* var */_needSchoolLevel: int;

        /* internal */ /* var */_maxStudyNum: int=2;

        /* internal */ /* var */_contribution: int;

        /* internal */ /* var */_schoolLevel: int;

        /* internal */ /* var */_studyNum: int;

        /* internal */ /* var */_propNum: int;
    }
